home *** CD-ROM | disk | FTP | other *** search
/ Shareware Super Platinum 8 / Shareware Super Platinum 8.iso / mac / PROGTOOL / GWMALLOC.ZIP;1 / GWMALLOC.TAR / gw_malloc / error.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-04-08  |  3.9 KB  |  150 lines

  1. /*
  2.  * error and message routines
  3.  *
  4.  * Copyright 1992 by Gray Watson and the Antaire Corporation
  5.  *
  6.  * This file is part of the malloc-debug package.
  7.  *
  8.  * This library is free software; you can redistribute it and/or
  9.  * modify it under the terms of the GNU Library General Public
  10.  * License as published by the Free Software Foundation; either
  11.  * version 2 of the License, or (at your option) any later version.
  12.  *
  13.  * This library is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16.  * Library General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU Library General Public
  19.  * License along with this library (see COPYING-LIB); if not, write to the
  20.  * Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  *
  22.  * The author of the program may be contacted at gray.watson@antaire.com
  23.  */
  24.  
  25. /*
  26.  * This file contains the routines needed for processing error codes
  27.  * produced by the compiler.
  28.  */
  29.  
  30. #include <fcntl.h>                /* for O_WRONLY, etc. */
  31. #include <signal.h>                /* for kill signals */
  32. #include <stdarg.h>                /* for message vsprintf */
  33.  
  34. #define MALLOC_DEBUG_DISABLE
  35.  
  36. #include "malloc.h"
  37. #include "malloc_loc.h"
  38.  
  39. #include "chunk.h"
  40. #include "compat.h"
  41. #include "conf.h"
  42. #include "dbg_values.h"
  43. #include "error.h"
  44.  
  45. #if INCLUDE_RCS_IDS
  46. LOCAL    char    *rcs_id =
  47.   "$Id: error.c,v 1.19 1993/04/05 22:30:07 gray Exp $";
  48. #endif
  49.  
  50. /*
  51.  * exported variables
  52.  */
  53. /* global debug flags that are set my MALLOC_DEBUG environ variable */
  54. EXPORT    int        _malloc_debug = 0;
  55.  
  56. /*
  57.  * message writer with printf like arguments
  58.  */
  59. EXPORT    void    _malloc_message(const char * format, ...)
  60. {
  61.   static int    outfile = -1;
  62.   int        len;
  63.   char        str[1024];
  64.   va_list    args;
  65.   
  66.   /* no logpath then no workie */
  67.   if (malloc_logpath == NULL
  68.       && ! BIT_IS_SET(_malloc_debug, DEBUG_PRINT_PERROR))
  69.     return;
  70.   
  71.   /* write the format + info into str */
  72.   va_start(args, format);
  73.   (void)vsprintf(str, format, args);
  74.   va_end(args);
  75.   
  76.   /* find the length of str, if empty then return */
  77.   len = strlen(str);
  78.   if (len == 0)
  79.     return;
  80.   
  81.   /* tack on a '\n' if necessary */
  82.   if (str[len - 1] != '\n') {
  83.     str[len++] = '\n';
  84.     str[len] = NULLC;
  85.   }
  86.   
  87.   /* do we need to log the message? */
  88.   if (BIT_IS_SET(_malloc_debug, DEBUG_LOG_PERROR) && malloc_logpath != NULL) {
  89.     /*
  90.      * do we need to open the outfile?
  91.      * it will be closed by _exit().  yeach.
  92.      */
  93.     if (outfile < 0) {
  94.       outfile = open(malloc_logpath, O_WRONLY | O_CREAT | O_TRUNC, 0666);
  95.       if (outfile < 0) {
  96.     (void)sprintf(str, "%s:%d: could not open '%s'\n",
  97.               __FILE__, __LINE__, malloc_logpath);
  98.     (void)write(STDERR, str, strlen(str));
  99.     exit(1);
  100.       }
  101.     }
  102.     
  103.     /* write str to the outfile */
  104.     (void)write(outfile, str, len);
  105.   }
  106.   
  107.   /* do we need to print the message? */
  108.   if (BIT_IS_SET(_malloc_debug, DEBUG_PRINT_PERROR)) {
  109.     (void)write(STDERR, str, strlen(str));
  110.   }
  111. }
  112.  
  113. /*
  114.  * kill the program because of an internal malloc error
  115.  */
  116. EXPORT    void    _malloc_die(void)
  117. {
  118.   /* do I need to drop core? */
  119.   if (BIT_IS_SET(_malloc_debug, DEBUG_ERROR_ABORT))
  120.     (void)kill(getpid(), SIGABRT);
  121.   
  122.   /*
  123.    * NOTE: this should not be exit() because fclose will free, etc
  124.    */
  125.   _exit(1);
  126. }
  127.  
  128. /*
  129.  * malloc version of perror of an error in STR
  130.  */
  131. EXPORT    void    _malloc_perror(const char * func)
  132. {
  133.   /* do we need to log or print the error? */
  134.   if ((BIT_IS_SET(_malloc_debug, DEBUG_LOG_PERROR) && malloc_logpath != NULL)
  135.       || BIT_IS_SET(_malloc_debug, DEBUG_PRINT_PERROR)) {
  136.     
  137.     /* default str value */
  138.     if (func == NULL)
  139.       func = "malloc_perror";
  140.     
  141.     /* print the malloc error message */
  142.     _malloc_message("ERROR: %s: %s(%d)",
  143.             func, malloc_strerror(malloc_errno), malloc_errno);
  144.   }
  145.   
  146.   /* do I need to abort? */
  147.   if (BIT_IS_SET(_malloc_debug, DEBUG_ERROR_ABORT))
  148.     _malloc_die();
  149. }
  150.